package com.fan.chat.component;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.List;

@Data
@Configuration
@ConfigurationProperties(prefix = "fan.chat")
public class ChatConfig {

    private static Proxy proxy = null;

    public String apiHost = "https://api.openai.com/";

    public List<String> apiKey;

    public List<String> fasterApiKey;

    private Proxy.Type proxyType;
    private String proxyIp;
    private Integer proxyPort;

    public synchronized Proxy getProxy() {
        if (proxyType == null || !StringUtils.hasLength(proxyIp) || proxyPort == null) {
            return Proxy.NO_PROXY;
        }
        if (proxy == null) {
            proxy = new Proxy(proxyType, new InetSocketAddress(proxyIp, proxyPort));
        }
        return proxy;
    }

}
